<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Salmon_Tag extends Salmon_Abstract {
    
    public $generatedAtom;
    
    public function createAtomEntryAndSendDoEverythingThisIsTemporarTest(Minime_User $actor, Discovery_Uri $discoverer, $targetURL, $targetTitle) {
        
        $msgTitle = $actor->getName() . " tagged " . $discoverer->getName() . " to photo " . $targetURL . ".";
        $msgContent = '<a href="' . $actor->getUri() . '">' . $actor->getName() . '</a> tagged <a href="' . $discoverer->getWebfingerProfilePageUri() . '">' . $discoverer->getName() . '</a> to photo <a href="' . $targetURL . '">' . $targetTitle . '</a>.';
        
        // Let's create the needed elements.
        $linkHref = Minime_HrefRenderer::getEntryLinkAlternate($actor->getNode(), time(), Minime::$conf);
        $id = new Atom_Entry_Id($linkHref);
        $title = new Atom_Entry_Title($msgTitle);
        $content = new Atom_Entry_Content($msgContent, Atom_Entry_Content::TYPE_HTML);
      
        $linkAlt = new Atom_Entry_Link_AlternateHtml($linkHref);
        
        $author = new Atom_Entry_Author();
        $author->addChildElement(new Atom_Entry_Author_Uri("acct:" . $actor->getJid()));
        $author->addChildElement(new Atom_Entry_Author_Name($actor->getName()));
        $author->addChildElement(new Atom_Entry_Author_Link_Photo($actor->getLogo(), "image/png"));
        $author->addChildElement(new Atom_Entry_Author_Link_AlternateHtml(Minime_HrefRenderer::getLdrrProfileHref($actor->getNode(), Minime::$conf), "image/png"));
        
        // Let's create the activity actor
//        $as_actor = new Activitystream_Actor();
//        $as_actor->addChildElement(new Activitystream_ObjectType(Activitystream_ObjectType::TYPE_PERSON));
//        
//        $as_actor->addChildElement(new Atom_Entry_Id($actor->getUri()));
//        $as_actor->addChildElement(new Atom_Entry_Title($actor->getName()));
//        $as_actor->addChildElement(new Atom_Entry_Link(Atom_Entry_Link::REL_ALTERNATE, 
//                                                       Atom_Entry_Link::TYPE_TEXTHTML, 
//                                                       $actor->getUri()));
//        $as_actor->addChildElement(new Atom_Entry_Link_Avatar($actor->getLogo()));
//        $as_actor->addChildElement(new Atom_Entry_Link_Photo($actor->getLogo()));

        // And let's create the activity verb
        $as_verb_tag = new Activitystream_Verb(Activitystream_Verb::VERB_TAG);
        //$as_verb_post = new Activitystream_Verb(Activitystream_Verb::VERB_POST);
        
        //$link_attention = new Atom_Entry_Link_Ostatus_Attention($discoverer->getFeedsAuthorUri());
        
        // Let's do the activity object, the dude
        $as_object = new Activitystream_Object();
        $as_object->addChildElement(new Activitystream_ObjectType(Activitystream_ObjectType::TYPE_PERSON));
        $as_object->addChildElement(new Atom_Entry_Title($discoverer->getName()));
        $as_object->addChildElement(new Atom_Entry_Id($discoverer->getFeedsAuthorUri()));
        
        $as_target = new Activitystream_Target();
        $as_target->addChildElement(new Activitystream_ObjectType(Activitystream_ObjectType::TYPE_PHOTO));
        $as_target->addChildElement(new Atom_Entry_Id($targetURL));
        $as_target->addChildElement(new Atom_Entry_Content($targetTitle, Atom_Entry_Content::TYPE_HTML));
        $as_target->addChildElement(new Atom_Entry_Link_AlternateHtml($targetURL));
        
        // Finally let's create the Atom Entry itself
        $atom = new Atom_Entry();
        
        $atom->addChildElement($id);
        $atom->addChildElement($linkAlt);
        $atom->addChildElement($title);
        $atom->addChildElement($content);
        $atom->addChildElement($author);
        $atom->addChildElement($link_attention);
        //$atom->addChildElement($as_actor);
        $atom->addChildElement($as_verb_tag);
        //$atom->addChildElement($as_verb_post);
        $atom->addChildElement($as_object);
        $atom->addChildElement($as_target);
        
        // Let's add needed new namespaces.
        $atom->addNamespace(ActivityStream::AS_NS_PREFIX, Activitystream::AS_NS);
        
        $signer = new Magic_Signature_Sign($actor->getEncodedPrivateKey());
        
        $encodedPayload = Magic::base64_url_encode($atom->asXML());
        
        $signature = $signer->sign($encodedPayload);
        
        $magicEnvelope = Magic_Envelope::build($encodedPayload, $signature);
        $envelope = $magicEnvelope->asXML();
        
        echo "\nReady to send the following atom (as decoded):\n";
        echo $atom->asXML();
        
        $this->generatedAtom = $atom;
        return $this->postSalmon($discoverer->getSalmonUri(), $envelope);       
    }
}